package com.NHL.single
{
	import com.NHL.consts.AnimationConst;
	import com.NHL.modules.mc.animation.McAnimation;
	import com.NHL.modules.mc.vo.AnimationVo;
	import com.greensock.core.Animation;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	public class AnimationLoader extends Loader
	{
		private var _loadArr:Array;
		private var _process:int;
		private var _starting:Boolean;
		private var _loadedArr:Array;
		private static var _inst:AnimationLoader;
		private var result:Function;
		public static function getInstance():AnimationLoader
		{
			return _inst||(_inst = new AnimationLoader());
		}
		public function AnimationLoader()
		{
			super();
			configLoader();
		}
		
		public function get loadArr():Array
		{
			return _loadArr;
		}

		public function set loadArr(value:Array):void
		{			
			_loadArr = value;				
		}
		
		public function start(result:Function):void
		{
			this.result = result
			if(!_process)
			{
				return;
			}
			else
			{
				
				_process = 0;
				var url:String = _loadArr[_process];
				configLoader();
				startLoad(url)
			}
		}
		
		private function startLoad(url:String):void
		{
			var lc:LoaderContext = new LoaderContext(false,new ApplicationDomain);
			load(new URLRequest(url),lc);
		}

		private function configLoader():void
		{
			contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError,false,0,true);
			contentLoaderInfo.addEventListener(NetStatusEvent.NET_STATUS,onError,false,0,true);
			contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onError,false,0,true);
			contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete,false,0,true);
			
		}
		
		private function unconfigLoader():void
		{
			contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onError);
			contentLoaderInfo.removeEventListener(NetStatusEvent.NET_STATUS,onError);
			contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,onError);
			contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete);
		}
		
		private function onError(evt:Event):void
		{
			_process = 0;
		}
		
		private function onComplete(evt:Event):void
		{
			_process++;
			
			if(_process == _loadArr.length)
			{
				_process = 0;
			//	initAnimation();
			}
			var length:int = getLength(evt);
			initAnimation(_loadArr[_process].id,length,evt.target.loader);
		}
		
		private function initAnimation(id:String,length:int,data:Object):void
		{
		 // var animation:McAnimation = ESource.getInstance().getAnimation(id);
		  var datalength:int = AnimationConst.LENGTH//animation.data.length;
		  var actlength:int = AnimationConst.ACTLENGTH; 
		  var dataVo:Vector.<AnimationVo> = new Vector.<AnimationVo>();
			for(var i:int = 0;i<datalength;i++)
			{
				var actType:String = AnimationConst.ACTACTARR[datalength/actlength]+ AnimationConst.ACTDIRARR[datalength%actlength];
				//animation.data[i];
				for(var j:int = 0; j < length; j++)
				{				
					var clazz:Class = (data as Loader).contentLoaderInfo.applicationDomain.getDefinition(actType+"_"+length) as Class;
					dataVo[i].data.push(new clazz());
				}
			}
			if(dataVo.length == datalength)
			{
				ESource.getInstance().addAnimation(id,dataVo);
				result();
			}
		}
		
		private function getLength(evt:Event):int
		{
			return 8;
		}
			
	}
}